<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\widgets\LinkPager;
use kartik\editable\Editable;

$this->title = Yii::t('app.order', '分配房号');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="order-room-list">

    <h2 class="page-header"><small><?= $product->name?> - <?= $product->code ?> - <?= Html::encode($this->title) ?></small>
		
        <?=
			($type ==1) ? 
			
				Html::a('切换至房型模式', ['list', 'product_id'=>$product->id, 'type'=>2], ['class' => 'btn btn-success btn-sm pull-right'])
				:
				Html::a('切换至订单模式', ['list', 'product_id'=>$product->id, 'type'=>1], ['class' => 'btn btn-info btn-sm pull-right']) ?>

	</h2>
		<!-- label class="pull-right"><input type="checkbox" name="change-room"  >&nbsp;未分配</label -->

	<table class="table ">
		<thead><tr><th>订单号</th> <th>备注</th> <th>房型</th> <th>入住人数</th> <th>房间号</th> </tr></thead>

		<?php foreach($all as $k=>$rooms){ ?>
			<?php $i=1;
				  $num = count($rooms);
			?>

			<?php foreach($rooms as $key=>$room){  ?>
			<tr>
				<?php  if($i == 1){ ?>
					<td rowspan=<?= $num ?> >
						<p><b><?= $room->order['code'] ?></b>&nbsp;&nbsp;
							<?= ($room->order->isRelation>1) ? '<i class="fa fa-link text-info" title="关联单标识"></i>' : '' ?>
						</p>
						<p><small>联系人 : <?= $room->order->username ?></small></p>
						<p><small>电  话 : <?= $room->order->phone ?></small></p>
						<p><small><?= isset($room->member->id) ? $room->member->profile->name : '(未设置)' ?></small></p>
					</td>
					<td rowspan=<?= $num ?> ><?= $room->order->remark ?></td>
				<?php } ?>
				<?php $i +=1; ?>
				<td>
					<?php
						$room_name = $room_icon = "";
						$data = [];
						if($product->stock == 1){ // 国内航线
							$room_icon = empty($room->merge_id) ? '' :  "<i class='fa fa-handshake-o red' title='拼房标识'></i>&nbsp;";
            				$room_name .= $room->productFinalRoom->room->name;
            				$room_name .= '  '.$room->productFinalRoom->room->code;
            				$room_name .= '  '.$room->productFinalRoom->people .'人间&nbsp;&nbsp;';
							// 获取所有房型
							$all = $product->productRoom;
							$data = \yii\helpers\ArrayHelper::map($all, 'id', function($model){ return $model->room->name . $model->room->code .'  ' . $model->people .'人';} );

						}elseif($product->stock == 2){ // 海外航线

							$t = json_decode($product->rooms, true);
							foreach($t as $key=>$val){
								$data[$key] = $val['name'];	
							}

							$room_icon = empty($room->merge_id) ? '' :  "<i class='fa fa-handshake-o red' title='拼房标识'></i>&nbsp;";
            				$room_name .= $t[$room->final_room_id]['name'];
						}
						//print_r($data);
						echo $room_icon;
					?>
    				<?php 
						echo Editable::widget([
						    'name'=>'final_room_id',
						    'header' => '舱房类型 -----------------------',
							'value' => $data[$room->final_room_id],
						    'data'=> $data,
    			    		'formOptions' => ['action'=>'/order/room/edit-final?id='.$room->id,'method'=>'get'],
						    'format' => Editable::FORMAT_BUTTON,
						    'inputType' => Editable::INPUT_DROPDOWN_LIST,
    		    			//'asPopover' => false,
						    //'inputType' => Editable::INPUT_SELECT2,
						    //'options' => ['class'=>'form-control'],
						    'editableValueOptions' => ['class' => 'text-danger']
    					]);
					?>
				</td>
				<td>
    				<?php echo Editable::widget([
    				    'name' => 'final_num',
						'value' => $room->final_num,
    				    'asPopover' => true,
    				    'header' => '入住人数',
    				    'size'=>'sm',
    				    'formOptions' => ['action'=>'/order/room/edit-final-num?id='.$room->id,'method'=>'get'],
    				    'options' => ['class'=>'form-control', 'placeholder'=>'请输入住人数'],
    				]);?> 人 &nbsp;

					<?php
						$customers = $room->orderRoomCustomer;
						if($customers){
							foreach($customers as $c){
								echo ' / '.$c->orderCustomer->first_name . $c->orderCustomer->last_name;
							}
						}

						// 拼房标识
						if($room->merge_id){
							$merge_room = $room->mergeRoom;		
							$customers = $merge_room->orderRoomCustomer;
							if($customers){
								foreach($customers as $c){
									echo ' / <font class="pin">'.$c->orderCustomer->first_name . $c->orderCustomer->last_name .'</font>'; 
								}   
							}
						}
					?>
				</td>
				<td>
    				<?php echo Editable::widget([
    				    'name' => 'code',
						'value' => $room->code,
    				    'asPopover' => true,
    				    'header' => '分配房号',
    				    'size'=>'sm',
    				    'formOptions' => ['action'=>'/order/room/edit-code?id='.$room->id,'method'=>'get'],
    				    'options' => ['class'=>'form-control', 'placeholder'=>'请输入房号'],
    				]);?>
				</td>
			</tr>
			<?php } ?>
		<?php } ?>

	</table>
	<!-- ?= LinkPager::widget(['pagination' => $pages]); ? -->
</div>
